package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.Pyq;
import com.example.entity.PyqComment;
import com.example.mapper.PyqCommentMapper;
import com.example.service.PyqService;
import com.example.mapper.PyqMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author Administrator
 * @description 针对表【wd_pyq】的数据库操作Service实现
 * @createDate 2023-01-25 12:18:27
 */
@Service
public class PyqServiceImpl extends ServiceImpl<PyqMapper, Pyq>
        implements PyqService {

    @Autowired
    private PyqMapper pyqMapper;

    @Autowired
    private PyqCommentMapper pyqCommentMapper;

    @Override
    public boolean addPyq(String userid, Pyq pyq) {

        //判断非空
        if (!StringUtils.isEmpty(pyq) && StringUtils.isEmpty(userid)) {
            //插入朋友圈内容
            int PyqInsert = pyqMapper.insert(pyq);

            //插入朋友圈再relation表中的和用户的关系
            PyqComment relation = new PyqComment();
            relation.setPyqId(pyq.getPyqId());
            relation.setUserId(userid);
            int relationInsert = pyqCommentMapper.insert(relation);

            if (PyqInsert > 0 && relationInsert > 0) {
                return true;
            } else {
                return false;
            }

        }

        return false;
    }

    @Override
    public boolean deletePyq(String pyqId) {

        if (!StringUtils.isEmpty(pyqId)) {
            //删除pyq表记录
            int i = pyqMapper.deleteById(pyqId);
            //删除pyq_comment表记录（朋友圈留言）
            QueryWrapper<PyqComment> wrapper = new QueryWrapper<>();
            wrapper.eq("pyq_id", pyqId);
            int delete = pyqCommentMapper.delete(wrapper);
            if (i > 0 && delete > 0) {
                return true;
            } else {
                return false;
            }
        }

        return false;
    }
}




